package com.yantong.service.impl;

import com.yantong.constant.BusinessCode;
import com.yantong.exception.BDException;
import com.yantong.mapper.ProductGradeMapper;
import com.yantong.pojo.ProductGrade;
import com.yantong.service.ProductGradeService;
import com.yantong.utils.PageUtils;
import com.yantong.utils.ValidateUtil;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 * @author huangshuai-728982210@qq.com
 * @date 2021/6/8 16:48
 */
@Service
public class ProductGradeServiceImpl implements ProductGradeService {

	@Autowired
	private ProductGradeMapper productGradeMapper;

	@Override
	public int saveProductGrade(ProductGrade productGrade) {
		if (productGrade == null) {
			return 0;
		}
		ProductGrade productGradeOld = productGradeMapper.selectByPrimaryKey(productGrade.getId());
		if (productGradeOld != null) {
			throw new BDException(BusinessCode.CENTRE_TERMINAL_EXIST);
		}
		return productGradeMapper.insertSelective(productGrade);
	}

	@Override
	public PageUtils<ProductGrade> listProductGrade(ProductGrade productGrade, int offset, int limit) {
		PageUtils<ProductGrade> pageUtils = new PageUtils<>();
		int total = productGradeMapper.countByAll(productGrade);
		if (total > 0) {
			pageUtils.setTotal(total);
			pageUtils.setRows(productGradeMapper.listByAll(productGrade,new RowBounds(offset, limit)));
		}
		return pageUtils;
	}

	@Override
	public int updateProductGrade(ProductGrade productGrade) {
		if (productGrade == null || productGrade.getId() == null) {
			return 0;
		}
		return productGradeMapper.updateByPrimaryKeySelective(productGrade);
	}

	@Override
	public int removeProductGrade(Long id) {
		if (ValidateUtil.isEmptyObjectOrString(id)) {
			return 0;
		}
		return productGradeMapper.deleteByPrimaryKey(id);
	}

	@Override
	public ProductGrade getProductGradeById(Long id) {
		if (id == null) {
			return null;
		}
		return productGradeMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<ProductGrade> selectByAll() {
		return productGradeMapper.selectByAll();
	}
}
